/* 弹窗对象 */
Yacon.widget.DetailModel = function(conText)
{
	this.body = document.body;
	Yacon.apply(this,conText);
	this.create();	
};

/* 弹窗对象方法 */
Yacon.widget.DetailModel.Extend(Yacon.widget.WinControl,{
	
	/* 构造 */
	create:function(){
		
		//确定父子关系
		this.createConnect();
		
		//DIV构造
		this.createDiv();
		
		//Frame构造
		this.createFrame();
		
		//按钮构造
		this.createButton();		
	},
	
	/* 确定父子关系 */
	createConnect:function(){
		
		if(window.parent.winModel){
        	this.parent = window.parent.winModel;
        	this.parent.child = this;
		}
		
	},
	
	/* 构造DIV */
	createDiv:function(){
		
		this.element = document.createElement("div");
		this.element.id = this.element.name = "detail_div";	
		
		this.element.style.zIndex = 100000;
		
		if(this.parent){
			this.addAttr("class", "detail_detail_div_enable");			
		}else{
			this.addAttr("class", "detail_div_enable");
			//首层居中
			if(window.locationType == "1"){
				this.element.style.left = this.styleLeft + "px";
				this.element.style.top = this.styleTop + "px";
			}
			//绝对定位
			else if(window.locationType == "2"){
				this.element.style.left = this.styleLeft + "px";
				this.element.style.top = this.styleTop + "px";
			}			
		}
		//无限扩展
		if(window.locationType == "0"){
			//居中
			this.autoLocate();
		}
		
	},
	
	/* 构造Frame */
	createFrame:function(){
		
		this.frame = new Yacon.widget.DetailFrame({
			            id:"detail_frame",
			            name:"detail_frame",
						width:this.width,
						height:this.height,
						url:this.url
					 });
	},
	
	/* 构造按钮 */
	createButton:function(){
		
		this.button_exit = new Yacon.widget.DetailButton({
								  id:"exit_button_img",
								  name:"exit_button_img",
								  normal:"/public/images/skin_blue/button/exit_1.png",
								  emphasis:"/public/images/skin_blue/button/exit_2.png",
								  className:"detail_exit_button"
							  });
	},
	
	/* 加载前执行方法 */
	beforeFunction:function(){
		if(this.arg.before){
			if(Yacon.isfireuserEvent(this.arg.before)){
				eval(this.arg.before+"();");
			}
		}
	},
	
	/* 加载后执行方法 */
	onloadFunction:function(){
        if(this.arg.onload){
			if(Yacon.isfireuserEvent(this.arg.onload)){
				eval(this.arg.onload+"();");
			}
		}
	},
	
	/* 最终执行方法 */
	afterFunction:function(){
		if(this.arg.after){
			if(Yacon.isfireuserEvent(this.arg.after)){
				eval(this.arg.after+"();");
			}
		}
	},
		
	/* 展示 */
	show:function(){
		
		//加载前执行
		this.beforeFunction();
	
	    //自动调整窗体大小
	    this.autoSize();
	    
	    //按钮定位
	    this.locateButton();
		
		//添加DIV
		this.body.appendChild(this.element);
		
		//添加Frame
        this.addElement(this.frame);
        
        if(this.isExitSupport == true){
            //添加按钮
            this.addElement(this.button_exit);
        }
        
        //Frame获取焦点
        this.frame.focus();
        
        //加载后执行
        this.onloadFunction();
        			
	},
	
	/* 自动调整窗体大小 */
	autoSize:function(){
		
        if(window == window.parent){}
        else{       	
        	if(this.parent){
	        	/* 自动调整高度 */
	        	this.parent.autoHeight(this.height);        	
	        	/* 自动调整宽度 */
	        	this.parent.autoWidth(this.width);
	        	/* 移除按钮 */
	        	this.parent.removeButton();
	        	
	        	//无限扩展
	        	if(window.locationType == "0"){
	    			//居中
	    			this.parent.autoLocate();
	    		}
	        	
	        	//首层扩展
	        	if(window.locationType == "1"){
	        		this.parent.autoLocateFirst();
	        	}
	        	
	        }
        }
	},
	
	/* 自动居中定位 */
	autoLocate:function(){
		
		//递归
		if(this.parent){
			this.parent.autoLocate();
		}
    	
		this.priAutoLocate();	
		this.element.style.left = this.styleLeft + "px";
		this.element.style.top = this.styleTop + "px";
    	
	},
	
	/* 自动定位上层 */
	autoLocateParent:function(){
		if(this.parent){
			this.parent.priAutoLocate();	
			this.parent.element.style.left = this.styleLeft + "px";
			this.parent.element.style.top = this.styleTop + "px";
		}
	},
	
	/* 首层居中定位 */
	autoLocateFirst:function(){
		if(this.parent){
			this.parent.autoLocateFirst();
		}else{
			this.priAutoLocate();	
			this.element.style.left = this.styleLeft + "px";
			this.element.style.top = this.styleTop + "px";
		}
			
	},
	
	/* 居中准备 */
	priAutoLocate:function(){
		
		var phasicHeight = document.body.scrollHeight;
    	var phasicWidth = document.body.scrollWidth;
    	
    	var imgWidth = Yacon.getPx(this.width);
    	var imgHeight = Yacon.getPx(this.height);
    	
    	this.styleLeft = 0;
    	this.styleTop = 0;
    	
    	if(imgWidth <= phasicWidth){
    		this.styleLeft = (phasicWidth - imgWidth) / 2;
    	}
    	
    	if(imgHeight <= phasicHeight){
    		this.styleTop = (phasicHeight - imgHeight) / 2;
    	}
    	
	},
	
	/* 自动调整高度  */
	autoHeight:function(height){		
		if(this.parent){
			this.parent.autoHeight(height);
		}
		
    	if(Yacon.getPx(height) > Yacon.getPx(this.height)){
    		this.height = height;
    		this.frame.setHeight(height);
    	}
	},
	
	/* 自动调整宽度 */
	autoWidth:function(width){
		
		if(this.parent){
			this.parent.autoWidth(width);
		}
		
    	if(Yacon.getPx(width) > Yacon.getPx(this.width)){
    		this.width = width;
    		this.frame.setWidth(width);
    		//定位按钮
    		this.locateButton();
    	}
			
	},
	
	/* 重置高度 */
	resetHeight:function(){
		if(Yacon.getPx(this.frame.height) != Yacon.getPx(this.height)){
			this.height = this.frame.height;
			this.frame.resetHeight();
			
		}
	},
	
	/* 重置宽度 */
	resetWidth:function(){
		if(Yacon.getPx(this.frame.width) != Yacon.getPx(this.width)){
			this.width = this.frame.width;
			this.frame.resetWidth();
			//定位按钮
			this.locateButton();
		}
	},
		
	/* 设置参数 */
	setArg:function(arg){				
		this.arg = arg;
	},
	
	
	/* 退出 */
	exit:function(){		
		this.destroy();			
	},
	
	/* 保存 */
	save:function(){		
		this.destroy();
	},
	
	/* 销毁方法 */
	destroy:function(){
		
		//赋值
		if(detail_frame.window.returnValue){
			this.returnValue = detail_frame.window.returnValue;
		}
		
		if(this.arg && this.arg.list)
			this.arg.list.refresh();
		
		this.body.removeChild(this.element);
		
		//函数回调
		this.afterFunction();
		
		//重置窗体
		if(this.parent){		
			this.parent.resetHeight();
			this.parent.resetWidth();
			this.parent.addButton();
			this.parent.focus();
			//复位方法 - 无限扩展
        	if(window.locationType == "0"){
    			//居中
    			this.autoLocate();
    		}
        	
        	//复位方法 - 首层复位
        	if(window.locationType == "1"){
        		this.autoLocateFirst();
        	}	
			
		}
		
		//遮罩层销毁
		window.pageLayer.hidden();
		
		//必须删除 -- 否则混合使用会报错
		delete window.winModel;
		
	},
	
	/* 移除按钮 */
	removeButton:function(){
		if(this.isExitSupport == true){
			this.removeElement(this.button_exit);
		}	
	},
	
	/* 添加按钮 */
	addButton:function(){
		if(this.isExitSupport == true){
			this.addElement(this.button_exit);
		}
	},
	
	/* 定位按钮 */
	locateButton:function(){
		if(this.isExitSupport == true){
			var imgWidth = Yacon.getPx(this.width) - 45 ; 	        
			this.button_exit.setStyle("left:"+imgWidth+"px");
		}
	},
	
	/* 获取焦点 */
	focus:function(){
		if(this.frame){
			this.frame.focus();
		}
	}
	
});

/* 弹窗 - Frame 组件 */
Yacon.widget.DetailFrame = function(conText){
	Yacon.apply(this,conText);
	this.create();
};

/* 弹窗 - Frame 方法 */
Yacon.widget.DetailFrame.Extend(Yacon.widget.WinControl,{
	
	/* 构造 */
	create:function(){
		this.element = document.createElement("iframe");
		this.element.id = this.id ;
		this.element.name = this.name ;
		this.element.width = this.width;
		this.element.height = this.height;
		this.element.src = this.url;
	},
	
	/* 重置高度 */
	resetHeight:function(){
		this.element.height = this.height;
	},
	
	/* 重置宽度 */
	resetWidth:function(){
		this.element.width = this.width;
	},
	
	/* 设置高度 */
	setHeight:function(height){
		this.element.height = height;
	},
	
	/* 设置宽度 */
	setWidth:function(width){
		this.element.width = width;
	},
	
	/* 获取焦点 */
	focus:function(){
		this.element.focus();
	}

});

/* 弹窗 - Button 组件 */
Yacon.widget.DetailButton = function(conText){
	Yacon.apply(this,conText);
	this.create();	
};

/* 弹窗 - Button 方法 */
Yacon.widget.DetailButton.Extend(Yacon.widget.WinControl,{
	
	/* 构造 */
	create:function(){
		this.element = document.createElement("img");
		this.element.id = this.id;
		this.element.name = this.name;
		this.addAttr("src", Yacon.getRootPath()+this.normal);
		this.addAttr("class", this.className);
		//添加动作
		this.addEvents(this);
	},
	
	/* 样式设置 */
	setStyle:function(style){
		this.addAttr("style",style);
	},
	
	/* 添加动作 */
	addEvents:function(button){
		
		this.element.onclick = function(){
        	window.winModel.exit();
        };
        
        this.element.onmouseover = function(){
        	this.src = Yacon.getRootPath() + button.emphasis;
        };
        
        this.element.onmouseout = function(){
        	this.src = Yacon.getRootPath() + button.normal;
        };
        
	}

});